const http = require("http");
const path = require("path");
const fs = require("fs");

const server = http.createServer();

server.on("request", (req, res) => {
  const reqUrl = req.url;
  const reqMethod = req.method;
  let fPath = "";

  if (reqUrl == "/") {
    fPath = path.join(__dirname, "/path/newHtml.html");
  } else {
    fPath = path.join(__dirname, "/path", reqUrl);
  }

  fs.readFile(fPath, "utf-8", (err, data) => {
    if (err) {
      res.end("404 not found");
      return;
    }
    res.setHeader("Content-Type", "text/html;charset=utf-8");
    res.end(data);
  });
});

server.listen(80, () => {
  console.log("server running 127.0.0.1");
});
